package com.ruoyi.web.controller.kycinfo;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.kycinfo.domain.*;
import com.ruoyi.kycinfo.service.*;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * kyc完善度Controller
 *
 * @author wzk
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/kycinfo/integrity")
public class KycIntegrityController extends BaseController {
    @Autowired
    private IKycIntegrityService kycIntegrityService;

    @Autowired
    private IKycFamilyService kycFamilyService;

    @Autowired
    private IKycPotentialService kycPotentialService;

    @Autowired
    private IKycProductService kycProductService;

    @Autowired
    private IKycUserInfoService kycUserInfoService;

    @Autowired
    private IKycWorkService kycWorkService;

    @Autowired
    private IKycService kycService;

    /**
     * 查询kyc完善度列表
     */

//    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(KycIntegrity kycIntegrity)
//    {
//        List<KycIntegrity> list = new ArrayList<>();
//        List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();
//
//        if (userInfoIdList.size() == 0) {
//            return getDataTable(list);
//        }
//
//        //计算当前用户能访问到的客户的信息完整度
//        //查询当前用户能访问到的客户kyc数据
////        List<KycUserInfo> kycUserInfoList= kycUserInfoService.selectKycUserInfoByUserInfoIdList(userInfoIdList);
//        List<KycUserInfo> kycUserInfoList= kycUserInfoService.selectKycUserInfoListByUserInfoIdList(userInfoIdList);
//
//        //更新全部导入系统的用户kyc完善度信息
//        for (KycUserInfo item : kycUserInfoList) {
//            KycIntegrity integrityItem = new KycIntegrity();
//            //个人总完善度
//            int totalintegrity = 0;
//
//            //更新家庭成员kyc完善度信息
//            KycFamily kycFamily = new KycFamily();
//            kycFamily.setUserInfoId(item.getUserId());
//            List<KycFamily> listkycfamily = kycFamilyService.selectKycFamilyList(kycFamily);
//            //只要查出一个家庭成员信息完善度即为2%
//            if (listkycfamily == null) {
//                kycIntegrity.setFamily("0");
//            } else {
//                integrityItem.setFamily("2");
//                totalintegrity = totalintegrity + 2;
//            }
//
//            //更新金融潜力kyc完善度信息
//            //金融潜力kyc完善度
//            int totalpotential = 0;
//            KycPotential kycPotential = kycPotentialService.selectKycPotentialByUserInfoId(item.getUserInfoId());
//            if (kycPotential != null) {
//                if (kycPotential.getPotentialName() != null) {
//                    totalpotential = totalpotential + 4;
//                    totalintegrity = totalintegrity + 4;
//                }
//                if (kycPotential.getRelationship() != null) {
//                    totalpotential = totalpotential + 3;
//                    totalintegrity = totalintegrity + 3;
//                }
//                if (kycPotential.getIsMyClient() != null) {
//                    totalpotential = totalpotential + 3;
//                    totalintegrity = totalintegrity + 3;
//                }
//            }
//            integrityItem.setPotential("" + totalpotential);
//
//            //更新产品kyc完善度信息
//            //产品kyc完善度
//            int totalproduct = 0;
//            KycProduct kycProduct = kycProductService.selectKycProductByUserInfoId(item.getUserInfoId());
//            if (kycProduct != null) {
//                if (kycProduct.getRisk() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalproduct = totalproduct + 3;
//                }
//                if (kycProduct.getKeyProduct() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalproduct = totalproduct + 3;
//                }
//                if (kycProduct.getMyLoan() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getOtherLoan() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getCreditCardCount() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getCreditCardType() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getSituation() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getDebitCardType() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getLoanRelevant() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getBind() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getBindInstitution() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalproduct = totalproduct + 2;
//                }
//                if (kycProduct.getDepositType() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totalproduct = totalproduct + 1;
//                }
//                if (kycProduct.getMoneyManage() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totalproduct = totalproduct + 1;
//                }
//                if (kycProduct.getFund() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totalproduct = totalproduct + 1;
//                }
//                if (kycProduct.getInsurance() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totalproduct = totalproduct + 1;
//                }
//                if (kycProduct.getNobleMetal() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totalproduct = totalproduct + 1;
//                }
//                if (kycProduct.getActivity() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totalproduct = totalproduct + 1;
//                }
//            }
//            integrityItem.setProduct("" + totalproduct);
//
//            //更新用户信息kyc完善度信息
//            //用户信息kyc完善度
//            int totaluserinfo = 0;
//            if (item != null) {
//                if (item.getName() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totaluserinfo = totaluserinfo + 3;
//                }
//                if (item.getGender() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getWeChat() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getContactTime() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getNativePlace() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getClienType() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getMarriage() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getEducation() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totaluserinfo = totaluserinfo + 2;
//                }
//                if (item.getHouseCount() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getCarCount() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getMedicalHistory() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getDietaryHabit() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getExerciseMethod() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getHobby() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getLivingNeed() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getConsumeHabit() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getTemper() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getTripMode() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//                if (item.getBelief() != null) {
//                    totalintegrity = totalintegrity + 1;
//                    totaluserinfo = totaluserinfo + 1;
//                }
//            }
//            integrityItem.setUserinfo("" + totaluserinfo);
//
//            //更新工作情况kyc完善度信息
//            //工作情况kyc完善度
//            int totalwork = 0;
//            KycWork kycWork = kycWorkService.selectKycWorkByUserInfoId(item.getUserInfoId());
//            if (kycWork != null) {
//                if (kycWork.getCompanyName() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getAddress() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getEvection() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getDepartment() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getCompanyPost() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getLevel() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getPassport() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getCompanyNature() != null) {
//                    totalintegrity = totalintegrity + 3;
//                    totalwork = totalwork + 3;
//                }
//                if (kycWork.getCountry() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalwork = totalwork + 2;
//                }
//                if (kycWork.getWorkExperience() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalwork = totalwork + 2;
//                }
//                if (kycWork.getLife() != null) {
//                    totalintegrity = totalintegrity + 2;
//                    totalwork = totalwork + 2;
//                }
//            }
//            integrityItem.setWork("" + totalwork);
//            integrityItem.setRemark("" + totalintegrity);
//            integrityItem.setUserInfoId(item.getUserInfoId());
//            integrityItem.setUserName(item.getName());
//
//            //判断完善度表是否含有数据
//            if(kycIntegrityService.selectKycIntegrityByUserInfoId(item.getUserInfoId())!=null){
//                kycIntegrityService.updateKycIntegrityByUserinfoId(integrityItem);
//            }else{
//                kycIntegrityService.insertKycIntegrity(integrityItem);
//            }
//
//        }
//
//        kycIntegrity.setUserInfoIdList(userInfoIdList);
//        startPage();
//        list = kycIntegrityService.selectKycIntegrityList(kycIntegrity);
//
//        return getDataTable(list);
//    }
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:list')")
    @GetMapping("/list")
    public TableDataInfo list(KycIntegrity kycIntegrity) {
        List<KycIntegrity> kycIntegrityList = new ArrayList<>();
        // 查询当前登陆用户可以访问到的所有的客户号
        List<Long> userInfoIdList = kycService.selectKycUserInfoIdList();

        // 如果可以访问到的客户号列表为空，直接返回空数据
        if (userInfoIdList.size() == 0) {
            return getDataTable(kycIntegrityList);
        }
        kycIntegrity.setUserInfoIdList(userInfoIdList);

        startPage();
        kycIntegrityList = kycIntegrityService.selectKycIntegrityList(kycIntegrity);
        for (KycIntegrity item : kycIntegrityList) {
            int family = 0;
            if (item.getFamily() != null) {
                family = Integer.parseInt(item.getFamily());
            }
            int potential = 0;
            if (item.getPotential() != null) {
                potential = Integer.parseInt(item.getPotential());
            }
            int product = 0;
            if (item.getProduct() != null) {
                product = Integer.parseInt(item.getProduct());
            }
            int work = 0;
            if (item.getWork() != null) {
                work = Integer.parseInt(item.getWork());
            }
            int userinfo = 0;
            if (item.getUserinfo() != null) {
                userinfo = Integer.parseInt(item.getUserinfo());
            }
            int total = family + potential + product + work + userinfo;
            item.setRemark(total + "");
        }
        return getDataTable(kycIntegrityList);
    }

    /**
     * 分行kyc完善度平均值
     * 计算所有客户每个kyc完善度的平均值
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:branchquery')")
    @GetMapping(value = "/branchquery")
    public AjaxResult branchQuery() {
        //查询全部kyc完善度信息
        KycIntegrity kycIntegrity = new KycIntegrity();
        List<KycIntegrity> list = kycIntegrityService.selectKycIntegrityList(kycIntegrity);
        //计算所有kyc总的完善度的平均值
        int total = 0;  //总的完善度
        int family = 0;  //家庭完善度
        int potential = 0;  //潜力完善度
        int product = 0;  //产品完善度
        int userinfo = 0;  //个人信息完善度
        int work = 0;  //工作情况完善度
        int count = 0;  //总的客户数量

        for (KycIntegrity item : list) {
            String totalstr = item.getRemark();
            String familystr = item.getFamily();
            String potentialstr = item.getPotential();
            String productstr = item.getProduct();
            String userinfostr = item.getUserinfo();
            String workstr = item.getWork();
            // 移除%符号
            String numbertotal = totalstr.replace("%", "");
            String familytotal = familystr.replace("%", "");
            String potentialtotal = potentialstr.replace("%", "");
            String producttotal = productstr.replace("%", "");
            String userinfototal = userinfostr.replace("%", "");
            String worktotal = workstr.replace("%", "");

            // 将字符串转为int类型
            int integrity = Integer.parseInt(numbertotal);
            int fam = Integer.parseInt(familytotal);
            int poten = Integer.parseInt(potentialtotal);
            int pro = Integer.parseInt(producttotal);
            int user = Integer.parseInt(userinfototal);
            int wor = Integer.parseInt(worktotal);
            total = total + integrity;
            family = family + fam;
            potential = potential + poten;
            product = product + pro;
            userinfo = userinfo + user;
            work = work + wor;
            count++;
        }
        total = total / count;  //总的完善度
        family = family / count;  //家庭完善度
        potential = potential / count;  //潜力完善度
        product = product / count;  //产品完善度
        userinfo = userinfo / count;  //个人信息完善度
        work = work / count;  //工作情况完善度
        KycIntegrity Integrity = new KycIntegrity();
        Integrity.setRemark(total + "%");
        Integrity.setFamily(family + "%");
        Integrity.setPotential(potential + "%");
        Integrity.setProduct(product + "%");
        Integrity.setUserinfo(userinfo + "%");
        Integrity.setWork(work + "%");
        return success(Integrity);
    }


    /**
     * 每个支行kyc完善度平均值
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:subbranchquery')")
    @GetMapping(value = "/subbranchquery")
    public AjaxResult subbranchQuery() {
        return success();
    }


    /**
     * 导出kyc完善度列表
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:export')")
    @Log(title = "kyc完善度 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KycIntegrity kycIntegrity) {
        List<KycIntegrity> list = kycIntegrityService.selectKycIntegrityList(kycIntegrity);
        ExcelUtil<KycIntegrity> util = new ExcelUtil<KycIntegrity>(KycIntegrity.class);
        util.exportExcel(response, list, "kyc完善度数据");
    }

    /**
     * 获取kyc完善度详细信息
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:query')")
    @GetMapping(value = "/{integrityId}")
    public AjaxResult getInfo(@PathVariable("integrityId") Long integrityId) {
        return success(kycIntegrityService.selectKycIntegrityByIntegrityId(integrityId));
    }


    /**
     * 新增kyc完善度
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:add')")
    @Log(title = "kyc完善度 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KycIntegrity kycIntegrity) {
        return toAjax(kycIntegrityService.insertKycIntegrity(kycIntegrity));
    }

    /**
     * 修改kyc完善度
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:edit')")
    @Log(title = "kyc完善度 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KycIntegrity kycIntegrity) {
        return toAjax(kycIntegrityService.updateKycIntegrity(kycIntegrity));
    }

    /**
     * 删除kyc完善度
     */
    @PreAuthorize("@ss.hasPermi('kycinfo:integrity:remove')")
    @Log(title = "kyc完善度 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{integrityIds}")
    public AjaxResult remove(@PathVariable Long[] integrityIds) {
        return toAjax(kycIntegrityService.deleteKycIntegrityByIntegrityIds(integrityIds));
    }


}
